import java.util.concurrent.TimeUnit;

public class main {
    static ThreadLocal<String> localString = new ThreadLocal<String>();
    public static void main(String[] args) {
        ThreadBean tb = new ThreadBean();

        new Thread(new MyRunnable(tb)).start();
        new Thread(new MyRunnable(tb)).start();
    }
    static class MyRunnable implements Runnable{
        ThreadBean tb;

        public MyRunnable(ThreadBean tb) {
            this.tb = tb;
        }

        public void run() {
            synchronized (tb){
                System.out.println(Thread.currentThread().getName());
                //sleep不会释放锁
//                try {
//                    TimeUnit.SECONDS.sleep(10);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                //wait会释放锁
//                try {
//                    tb.wait(20000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                //join会释放锁，底层调用的是this的wait，但是注意这里只会释放this的锁将上面的tb换成this就能释放，如果用tb就不能释放
                try {
                    Thread.currentThread().join(20000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                localString.set(Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getName()+" end "+localString.get());
            }
        }
    }
}
